﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Media;
using System.IO;

public class AudioManager : GameComponent
{
    private static AudioManager audioManager = null;

    Song playingMusic;
    string playingMusicName = "";
    ContentManager content;
    SoundEffectInstance soundInstance = null;
    public static float SoundVolume = 0.6f;

    List<string> musicList = new List<string>();

    int actualMusic = 0;

    private static float musicVolume = 1.0f;
    public static float MusicVolume
    {
        get { return musicVolume; }
        set
        {
            musicVolume = value;
            MediaPlayer.Volume = musicVolume;
        }
    }

    private AudioManager(Game game) : base(game)
    {
        content = game.Content;
    }

    public static void Initialize(Game game)
    {
        audioManager = new AudioManager(game);
        if (game != null)
        {
            game.Components.Add(audioManager);
        }

        //audioManager.musicList.Add(@"Audio/Musica Menus");

        audioManager.musicList.Add(@"Audio/Musica 2");
        audioManager.musicList.Add(@"Audio/Musica Menus");

        PlayMusic(audioManager.musicList[audioManager.actualMusic]);
        audioManager.actualMusic++;
    }

    public static void PlaySound(string sound)
    {
        if (!Globals.Mute)
        {
            SoundEffect soundEffect = audioManager.content.Load<SoundEffect>(sound);
            audioManager.soundInstance = soundEffect.CreateInstance();
            audioManager.soundInstance.Play();// Play(SoundVolume, 0, 0);
        }
    }

    public static void PlayMusic()
    {
        /*if (!audioManager.playingMusicName.Equals(music))
        {
            if(audioManager.playingMusic != null)
                audioManager.playingMusic.Stop();
            SoundEffect soundEffect = audioManager.content.Load<SoundEffect>(music);
            audioManager.playingMusicName = music;
            audioManager.playingMusic = soundEffect.CreateInstance();
            audioManager.playingMusic.IsLooped = true;
            audioManager.playingMusic.Volume = MusicVolume;
            if (!Globals.Mute)
                audioManager.playingMusic.Play();
        }
        if(audioManager.playingMusic.State == SoundState.Paused)
        {
            audioManager.playingMusic.Play();
        }*/
    }

    public static void PlayMusic(string music)
    {
        if (!audioManager.playingMusicName.Equals(music))
        {
            if (MediaPlayer.State == MediaState.Playing)
                MediaPlayer.Stop();
            Song soundEffect = audioManager.content.Load<Song>(music);
            MediaPlayer.Volume = MusicVolume;
            MediaPlayer.Play(soundEffect);
        }
    }

    /*public static void PlayMusic(string music)
    {
        if (!audioManager.playingMusicName.Equals(music))
        {
            if (audioManager.playingMusic != null)
                audioManager.playingMusic.Stop();
            SoundEffect soundEffect = audioManager.content.Load<SoundEffect>(music);
            audioManager.playingMusicName = music;
            audioManager.playingMusic = soundEffect.CreateInstance();
            audioManager.playingMusic.IsLooped = false;
            audioManager.playingMusic.Volume = MusicVolume;
            if (!Globals.Mute)
                audioManager.playingMusic.Play();
        }
        if (audioManager.playingMusic.State == SoundState.Paused)
        {
            audioManager.playingMusic.Play();
        }
    }*/

    public static void StopMusic()
    {
        //audioManager.playingMusic.Stop();
        MediaPlayer.Stop();
    }

    public static void ContinueMusic()
    {
        //audioManager.playingMusic.Play();
    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);

        if(MediaPlayer.State == MediaState.Stopped)
        {
            PlayMusic(musicList[audioManager.actualMusic]);
            audioManager.actualMusic++;
            if (audioManager.actualMusic >= audioManager.musicList.Count)
                audioManager.actualMusic = 0;
        }
    }


    internal static void StopSound()
    {
        audioManager.soundInstance.Stop();
    }
}